package com.jackstraw.create.utils;

import java.io.*;
import java.net.MalformedURLException;

/**
 * 所在包：com.jackstraw.create.utils
 * 文件名：
 * 项目：create
 * 功能描述：
 * 修改人：Xlz
 * 修改时间：2021-12-08 9:47
 *
 * @author jackstraw
 */
public class PageUtil {

    public static void main(String[] args) throws IOException {
        String basePath = "E:\\temp\\views";
        String folderPath = "E:\\temp\\com\\example\\server\\pojo";
        String editPath = "E:\\temp\\com\\example\\server\\vo";
        createFolder(basePath, folderPath);
        copyIndex(basePath, folderPath);
        copyEditForm(basePath, editPath);
    }

    /**
     * 生成index文件
     * @param basePath
     * @param folderPath
     * @throws IOException
     */
    private static void copyIndex(String basePath, String folderPath) throws IOException {
        File folder = new File(folderPath);
        File[] files = folder.listFiles();
        for (File file : files) {
            String fileName = file.getName();
            String fileNameWithOutSuffix = fileName.substring(0, fileName.indexOf("."));
            File indexFile = new File(basePath + "\\" + fileNameWithOutSuffix + "\\index.vue");
            copyFile(file, indexFile);
        }
    }

    /**
     * 生成编辑窗内容
     * @param basePath
     * @param folderPath
     * @throws IOException
     */
    private static void copyEditForm(String basePath, String folderPath) throws IOException {
        File folder = new File(folderPath);
        File[] files = folder.listFiles();
        for (File file : files) {
            String fileName = file.getName();
            String fileNameWithOutSuffix = fileName.substring(0, fileName.indexOf(".") - 2);
            File indexFile = new File(basePath + "\\" + fileNameWithOutSuffix + "\\EditForm.vue");
            copyFile(file, indexFile);
        }
    }

    /**
     * 拷贝文件
     * @param sourceFile
     * @param targetFile
     * @throws IOException
     */
    private static void copyFile(File sourceFile, File targetFile) throws IOException {
        //输入流
        InputStream in = new FileInputStream(sourceFile);
        //输出流
        OutputStream out = new FileOutputStream(targetFile, true);

        try {
            byte[] buffer = new byte[1024];
            while (true) {
                int byteRead = in.read(buffer);
                if (byteRead == -1) {
                    break;
                }
                out.write(buffer, 0, byteRead);
            }
        }

        catch (MalformedURLException ex) {
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * 生成页面文件夹
     * @param basePath
     * @param folderPath
     */
    private static void createFolder(String basePath, String folderPath) {
        File folder = new File(folderPath);
        File[] files = folder.listFiles();
        for (File file : files) {
            String fileName = file.getName();
            String fileNameWithOutSuffix = fileName.substring(0, fileName.indexOf("."));
            File targetFile = new File(basePath + "\\" + fileNameWithOutSuffix);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
        }
    }
}
